﻿Public Class frmEditContact

    Public Enum Mode
        ADD
        EDIT
    End Enum

    Public Property contactId As Long
    Public Property contact As Contact
    Public Property contactService As ContactService

    Private Sub lnkAddNewEmail_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAddNewEmail.LinkClicked
        frmEmail.ShowDialog()
    End Sub

    Private Sub lnkAddNewPhone_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAddNewPhone.LinkClicked
        frmPhone.ShowDialog()
    End Sub

    Private Sub LinkLabel1_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
        frmAddress.ShowDialog()
    End Sub

    Private Sub frmEditContact_Shown(sender As Object, e As System.EventArgs) Handles Me.Shown

        If getMode() = Mode.EDIT Then
            setupForEditMode()
        Else
            setupForAddMode()
        End If

        ContactBindingSource.DataSource = contact
        dropTitle.Focus()

    End Sub

    Private Function getMode() As Mode
        Return If(contactId <> -1, Mode.EDIT, Mode.ADD)
    End Function

    Private Sub setupForEditMode()

        Me.contact = contactService.getContactById(contactId)
        refreshEmailAddresses()
        refreshPhones()
        refreshAddresses()

    End Sub

    Private Sub setupForAddMode()

        Me.contact = New Contact()
        refreshEmailAddresses()
        refreshPhones()
        refreshAddresses()

    End Sub

    Public Sub refreshEmailAddresses()
        bindEntities(dgEmails, contact.emailList)
    End Sub

    Public Sub refreshPhones()
        bindEntities(dgPhones, contact.phoneList)
    End Sub

    Public Sub refreshAddresses()
        bindEntities(dgAddresses, contact.addressList)
    End Sub

    Public Sub bindEntities(Of T)(dataGrid As DataGridView, entityList As List(Of T))

        Dim bs As BindingSource = New BindingSource
        bs.DataSource = entityList
        dataGrid.AutoGenerateColumns = False
        dataGrid.AutoSize = False
        dataGrid.DataSource = bs

    End Sub


    Private Sub dropTitle_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles dropTitle.SelectedIndexChanged
        ' Temp hack as two way databinding not working for drop down
        contact.title = dropTitle.SelectedItem
    End Sub

    Private Sub dgEmails_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgEmails.CellContentClick

        If e.RowIndex = -1 Then
            ' User clicked header, not supporting sorting yet
            Return
        End If

        Dim email As Email = TryCast(dgEmails.Rows(e.RowIndex).DataBoundItem, Email)
        Dim action As String = dgEmails(e.ColumnIndex, e.RowIndex).Value
        If (action = "Edit") Then
            frmEmail.email = email
            frmEmail.ShowDialog()
        ElseIf (action = "Delete") Then
            dgEmails.Rows.RemoveAt(e.RowIndex)
            refreshEmailAddresses()
        End If

    End Sub

    Private Sub dgPhones_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgPhones.CellContentClick

        If e.RowIndex = -1 Then
            ' User clicked header, not supporting sorting yet
            Return
        End If

        Dim phone As PhoneNumber = TryCast(dgPhones.Rows(e.RowIndex).DataBoundItem, PhoneNumber)
        Dim action As String = dgPhones(e.ColumnIndex, e.RowIndex).Value
        If (action = "Edit") Then
            frmPhone.phone = phone
            frmPhone.ShowDialog()
        ElseIf (action = "Delete") Then
            dgPhones.Rows.RemoveAt(e.RowIndex)
            refreshPhones()
        End If

    End Sub

    Private Sub dgAddresses_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgAddresses.CellContentClick

        If e.RowIndex = -1 Then
            ' User clicked header, not supporting sorting yet
            Return
        End If

        Dim address As Address = TryCast(dgAddresses.Rows(e.RowIndex).DataBoundItem, Address)
        Dim action As String = dgAddresses(e.ColumnIndex, e.RowIndex).Value
        If (action = "Edit") Then
            frmAddress.address = address
            frmAddress.ShowDialog()
        ElseIf (action = "Delete") Then
            dgAddresses.Rows.RemoveAt(e.RowIndex)
            refreshAddresses()
        End If

    End Sub

    Private Sub saveContact() Handles btnSave.Click

        Dim isSuccess As Boolean = contactService.saveContact(contact)
        If (isSuccess) Then
            frmLandingPage.displayMessageOfType(frmLandingPage.MessageType.SUCCESS, "Contact has been Updated.")
        End If
        frmLandingPage.loadContacts()
        Me.Hide()

    End Sub

    Private Sub lnkCancel_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkCancel.LinkClicked
        Me.Close()
    End Sub

End Class